/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using IQIGame.Onigao.Framework;
using System.Collections.Generic;
using System.Linq;
using System;
using NetProtocol.Enum;

namespace NetProtocol.POD {

	/// <summary>
	///  玩家自己, 这里会有冗余数据, 系统和关卡都会有一份, 因为关卡内可能会临时改变, 所以要这样设计, 以后判断的标准就是会临时改变的都做冗余, 其他的都直接读系统数据
	/// module : levelLogic
	/// </summary>
	public class LevelPlayerPOD : IPOD 
    {
        public readonly static Func<LevelPlayerPOD> FACTORY = () => new LevelPlayerPOD();
		public static Func<LevelPlayerPOD> DynamicFactory = FACTORY;
        
		/*-- members --*/
		/// <summary>
        /// 
        /// </summary>
		public long id;
		/// <summary>
        /// 
        /// </summary>
		public string name;
		/// <summary>
        /// 
        /// </summary>
		public int level;
		/// <summary>
        /// 
        /// </summary>
		public int exp;
		/// <summary>
        /// 
        /// </summary>
		public int avatar;
		/// <summary>
        /// 
        /// </summary>
		public int avatarFrame;
		/// <summary>
        /// 
        /// </summary>
        private List<LevelHeroPOD> m_heroes;
		/// <summary>
        /// 
        /// </summary>
		public long leaderHeroId;
		/// <summary>
        /// 
        /// </summary>
        private List<LevelBuffPOD> m_buffs;
		/// <summary>
        /// 
        /// </summary>
		public int followerId;
		/// <summary>
        /// 策划自定义相机参数
        /// </summary>
		public int customCameraCid;
		/// <summary>
        /// 
        /// </summary>
		public bool isQuickBarDisabled;
		
		
        public List<LevelHeroPOD> heroes
        {
            get
            {
                if(m_heroes == null)
                {
                    m_heroes = new();
                }
                return m_heroes;
            }
            private set => m_heroes = value;
        }
        public List<LevelHeroPOD> unsafe_heroes
        {
        	get => m_heroes;
        	set => m_heroes = value;
        }
        public List<LevelBuffPOD> buffs
        {
            get
            {
                if(m_buffs == null)
                {
                    m_buffs = new();
                }
                return m_buffs;
            }
            private set => m_buffs = value;
        }
        public List<LevelBuffPOD> unsafe_buffs
        {
        	get => m_buffs;
        	set => m_buffs = value;
        }
		
		/*-- constructors --*/
		public LevelPlayerPOD() {}
		public LevelPlayerPOD(long id, string name, int level, int exp, int avatar, int avatarFrame, List<LevelHeroPOD> heroes, long leaderHeroId, List<LevelBuffPOD> buffs, int followerId, int customCameraCid, bool isQuickBarDisabled) {
			this.id = id;
			this.name = name;
			this.level = level;
			this.exp = exp;
			this.avatar = avatar;
			this.avatarFrame = avatarFrame;
			this.m_heroes = heroes;
			this.leaderHeroId = leaderHeroId;
			this.m_buffs = buffs;
			this.followerId = followerId;
			this.customCameraCid = customCameraCid;
			this.isQuickBarDisabled = isQuickBarDisabled;
		}
	      public void Serial(SOutputStream _out) {
            _out.Write_long(id);
            _out.Write_string(name);
            _out.Write_int(level);
            _out.Write_int(exp);
            _out.Write_int(avatar);
            _out.Write_int(avatarFrame);
            _out.Write_list_pod(m_heroes);
            _out.Write_long(leaderHeroId);
            _out.Write_list_pod(m_buffs);
            _out.Write_int(followerId);
            _out.Write_int(customCameraCid);
            _out.Write_boolean(isQuickBarDisabled);
        }
        
        public void UnSerial(SInputStream _in) {
            if(!_in.HasRemaining()) return;
            id = _in.Read_long();
            if(!_in.HasRemaining()) return;
            name = _in.Read_string();
            if(!_in.HasRemaining()) return;
            level = _in.Read_int();
            if(!_in.HasRemaining()) return;
            exp = _in.Read_int();
            if(!_in.HasRemaining()) return;
            avatar = _in.Read_int();
            if(!_in.HasRemaining()) return;
            avatarFrame = _in.Read_int();
            if(!_in.HasRemaining()) return;
            m_heroes = _in.Read_list_pod<LevelHeroPOD>(LevelHeroPOD.DynamicFactory);
            if(!_in.HasRemaining()) return;
            leaderHeroId = _in.Read_long();
            if(!_in.HasRemaining()) return;
            m_buffs = _in.Read_list_pod<LevelBuffPOD>(LevelBuffPOD.DynamicFactory);
            if(!_in.HasRemaining()) return;
            followerId = _in.Read_int();
            if(!_in.HasRemaining()) return;
            customCameraCid = _in.Read_int();
            if(!_in.HasRemaining()) return;
            isQuickBarDisabled = _in.Read_boolean();
        }
		
		public LevelPlayerPOD Clone() {
            LevelPlayerPOD o = DynamicFactory();
            o.id = id;
            o.name = name;
            o.level = level;
            o.exp = exp;
            o.avatar = avatar;
            o.avatarFrame = avatarFrame;
            if (m_heroes != null) {
                o.m_heroes = m_heroes.ConvertAll(o => o?.Clone());
            }
            o.leaderHeroId = leaderHeroId;
            if (m_buffs != null) {
                o.m_buffs = m_buffs.ConvertAll(o => o?.Clone());
            }
            o.followerId = followerId;
            o.customCameraCid = customCameraCid;
            o.isQuickBarDisabled = isQuickBarDisabled;
            return o;
        }
        
        public void Clear() {
            id = default;
            name = default;
            level = default;
            exp = default;
            avatar = default;
            avatarFrame = default;
            m_heroes = default;
            leaderHeroId = default;
            m_buffs = default;
            followerId = default;
            customCameraCid = default;
            isQuickBarDisabled = default;
        }
		
        public override string ToString() {
			string str = "";
			str += "LevelPlayerPOD [";
			str += "id=";
			str += id;
			str += ", name=";
			str += name;
			str += ", level=";
			str += level;
			str += ", exp=";
			str += exp;
			str += ", avatar=";
			str += avatar;
			str += ", avatarFrame=";
			str += avatarFrame;
			str += ", heroes=";
			str += m_heroes.ToString<LevelHeroPOD>();
			str += ", leaderHeroId=";
			str += leaderHeroId;
			str += ", buffs=";
			str += m_buffs.ToString<LevelBuffPOD>();
			str += ", followerId=";
			str += followerId;
			str += ", customCameraCid=";
			str += customCameraCid;
			str += ", isQuickBarDisabled=";
			str += isQuickBarDisabled;
			str += "]";
			return str;
		}
		
	}
}